﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WiseLink.RDS.Model.Common;
using WiseLink.RDS.Model;

namespace WiseLink.RDS.Model
{
    public partial class DiagnosticRepository : RDSRepositoryBase
    {

        public DiagnosticRepository() : base()
        {
        }

        public IEnumerable<DiagnosticModel> GetDiagnosticInfo(bool isConfirmed)
        {
            var query = from v in ccf20Ent.Vehicles
                        where v.Devices.FirstOrDefault().IsRegistered == true 
                        orderby v.LatestActiveTime descending
                        select v;
            var query1 = from q in query.ToList().Where(v=>v.UnConfirmedFaultCount>0 || v.UrgentCount>0 || v.MaintenanceCount >0)
                        select new DiagnosticModel
                        {
                            Fault = q.FaultCount,
                            Urgent = q.UrgentCount,
                            Maintenance = q.MaintenanceCount,
                            Name = q.Customer.Name,
                            SerialNo = q.Devices.FirstOrDefault().DeviceKey,
                            VehicleID = q.VehicleID
                        };
            return query1;

        }
        private IQueryable<FaultModel> GetFaultByVehicle(int? vehicleID, bool IsActive, string faultType)
        {
            var query = from f in ccf20Ent.Faults
                        where (f.IsActive == IsActive &&
                               f.VehicleID == vehicleID &&
                               f.FaultType == faultType)
                        orderby f.HappenDate descending
                        select new FaultModel
                        {
                            IDCSimNumber = f.IDCSimNumber,
                            Code = f.Code,
                            CodeDescription = f.CodeDescription,
                            FaultID = (int)f.FaultID,
                            HappenDate = f.HappenDate,
                            IsConfirmed = f.IsConfirmed
                        };

            return query;
        }

        public IQueryable<MaintenanceModel> GetMaintenanceByVehicle(int? vehicleID)
        {
            var query = from f in ccf20Ent.VehicleMaintenanceHistories
                        where (f.IsActive == true &&
                               f.VehicleID == vehicleID)
                        orderby f.HappenDate descending
                        select new MaintenanceModel
                        {
                           CurrentMileage = f.CurrentMileage,
                           LastServiceDate = f.LastServiceDate,
                           LastServiceMileage = f.LastServiceMileage,
                           MaintenanceTypeName = f.MaintenanceType.Name,
                           ServiceMileage = f.MaintenanceType.ServiceMileage,
                           ServicePeriod = f.MaintenanceType.ServicePeriod,
                           MaintenanceID = f.VehicleMainTenanceHistoryID,
                           HappenDate = f.HappenDate
                        };

            return query;
        }

        public IQueryable<PhoneContactModel> GetContactHistoryByVehicle(int? vehicleID)
        {

            var query = from c in ccf20Ent.ContactHistories
                        where (c.IsActive && c.VehicleID == vehicleID)
                        orderby c.ContactTime descending
                        select new PhoneContactModel
                        {
                            ContactID = c.ContactHistoryID,
                            ContactTime = c.ContactTime,
                            ContactResult = c.ContactResult,
                            Operator = c.Operator,
                            Remark = c.Remark
                        };

                return query;
            
        }
        //车辆诊断模块，未确认过的故障码
        public IQueryable <FaultModel> GetUnconfirmedFaultByVehicle(int? VehicleID)
        {
            return GetFaultByVehicle(VehicleID, true, FaultType.Fault);
        }

          //"vehicleID=" + vehicleID + "&contactResult=" + $("#Customers").data("tDropDownList").value() + "&contactRemark=" + $("#contactRemark")[0].value,
        

        
    }
}
